package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.*;
import com.easyui.common.*;

public class GetUserInfoServlet extends HttpServlet {

	public GetUserInfoServlet() {
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		List<Map> infolist = null;
		JSONArray jsonInfo = null;

		HttpSession session = request.getSession(); // 获的session
		String role = (String) session.getAttribute("role");
		String userID = (String) session.getAttribute("userID");

		if (role != null && !role.trim().equals("")) {
			if (userID != null && !userID.trim().equals("")) {// session未过期
				infolist = new UserLogin().getUserInfo(role, userID);

				if (infolist != null && infolist.size() > 0) {
					Map<String, Object> map = infolist.get(0);
					jsonInfo = new JSONArray();

					if (role.equals("teacher")) {
						JSONObject tnoData = new JSONObject();
						tnoData.put("name", "工号");
						tnoData.put("value", map.get("tno"));
						tnoData.put("group", "教师个人信息");
						jsonInfo.add(tnoData);

						JSONObject tnameData = new JSONObject();
						tnameData.put("name", "姓名");
						tnameData.put("value", map.get("tname"));
						tnameData.put("group", "教师个人信息");
						jsonInfo.add(tnameData);

						JSONObject sexData = new JSONObject();
						sexData.put("name", "性别");
						sexData.put("value", map.get("sex"));
						sexData.put("group", "教师个人信息");
						jsonInfo.add(sexData);
					} else if (role.equals("student")) {
						JSONObject snoData = new JSONObject();
						snoData.put("name", "学号");
						snoData.put("value", map.get("sno"));
						snoData.put("group", "学生个人信息");
						jsonInfo.add(snoData);

						JSONObject snameData = new JSONObject();
						snameData.put("name", "姓名");
						snameData.put("value", map.get("sname"));
						snameData.put("group", "学生个人信息");
						jsonInfo.add(snameData);

						JSONObject sexData = new JSONObject();
						sexData.put("name", "性别");
						sexData.put("value", map.get("sex"));
						sexData.put("group", "学生个人信息");
						jsonInfo.add(sexData);

						JSONObject majorData = new JSONObject();
						majorData.put("name", "专业");
						majorData.put("value", map.get("major"));
						majorData.put("group", "学生个人信息");
						jsonInfo.add(majorData);

						JSONObject gradeData = new JSONObject();
						gradeData.put("name", "年级");
						gradeData.put("value", map.get("grade"));
						gradeData.put("group", "学生个人信息");
						jsonInfo.add(gradeData);

						JSONObject stypeData = new JSONObject();
						stypeData.put("name", "分工");
						stypeData.put("value", map.get("stype"));
						stypeData.put("group", "学生个人信息");
						jsonInfo.add(stypeData);
					}
				}
			}
		}

		out.print(jsonInfo);
		out.flush();
		out.close();
	}

}
